package com.example.springbooteasycode.utils.tree;

import java.util.*;

public class TreeBuilder {
    public static List<CommunityDeviceTypeTreeVO> buildTree(List<CommunityDeviceTypeTreeVO> nodes) {
        // 创建一个映射，用于快速查找节点
        Map<String, CommunityDeviceTypeTreeVO> nodeMap = new HashMap<>();
        for (CommunityDeviceTypeTreeVO node : nodes) {
            nodeMap.put(node.getDeviceTypeCode(), node);
        }

        // 创建一个列表，用于存储根节点
        List<CommunityDeviceTypeTreeVO> rootNodes = new ArrayList<>();

        // 遍历所有节点，构建树形结构
        for (CommunityDeviceTypeTreeVO node : nodes) {
            String parentCode = node.getParentCode();
            if (parentCode == null || parentCode.isEmpty()) {
                // 如果没有父节点，则该节点是根节点
                rootNodes.add(node);
            } else {
                // 查找父节点，并将当前节点添加到父节点的子节点列表中
                CommunityDeviceTypeTreeVO parentNode = nodeMap.get(parentCode);
                if (parentNode != null) {
                    if (parentNode.getChildren() == null) {
                        parentNode.setChildren(new ArrayList<>());
                    }
                    parentNode.getChildren().add(node);
                }
            }
        }

        return rootNodes;
    }

    public static void main(String[] args) {
        // 示例数据
        List<CommunityDeviceTypeTreeVO> nodes = Arrays.asList(
                new CommunityDeviceTypeTreeVO(1L, "001", "C001", "C1", "S001", "Root1", null, "001", 1, "1", 1, "1", "#001", "0",null),
                new CommunityDeviceTypeTreeVO(2L, "002", "C001", "C1", "S001", "Child1", "001", "001", 2, "1", 2, "1", "#001#002", "0",null),
                new CommunityDeviceTypeTreeVO(3L, "003", "C001", "C1", "S001", "Child2", "001", "001", 3, "1", 2, "1", "#001#003", "0",null),
                new CommunityDeviceTypeTreeVO(4L, "004", "C001", "C1", "S001", "SubChild1", "002", "001", 4, "0", 3, "1", "#001#002#004", "0",null),
                new CommunityDeviceTypeTreeVO(5L, "005", "C001", "C1", "S001", "Root2", null, "005", 5, "1", 1, "1", "#005", "0",null),
                new CommunityDeviceTypeTreeVO(6L, "006", "C001", "C1", "S001", "Child3", "005", "005", 6, "1", 2, "1", "#005#006", "0",null),
                new CommunityDeviceTypeTreeVO(7L, "007", "C001", "C1", "S001", "SubChild2", "006", "005", 7, "0", 3, "1", "#005#006#007", "0",null)
        );

        // 构建树形结构
        List<CommunityDeviceTypeTreeVO> tree = buildTree(nodes);
        System.out.println("Tree: " + tree);

        // 打印树形结构
        printTree(tree, 0);
    }

    public static void printTree(List<CommunityDeviceTypeTreeVO> nodes, int level) {
//        for (CommunityDeviceTypeTreeVO node : nodes) {
//            System.out.println(" ".repeat(level * 4) + node.getName() + " (" + node.getDeviceTypeCode() + ")");
//            if (node.getChildren() != null && !node.getChildren().isEmpty()) {
//                printTree(node.getChildren(), level + 1);
//            }
//        }
    }
}
